package cn.com.startai.huadiantrainer.mutual;

import android.app.Application;

import cn.com.startai.common.utils.CThreadPoolUtils;
import cn.com.startai.common.utils.CUtils;
import cn.com.startai.huadiantrainer.global.Debuger;
import cn.com.startai.huadiantrainer.global.LooperManager;
import cn.com.startai.huadiantrainer.global.speak.SpeakManager;
import cn.com.startai.huadiantrainer.js.ICallJS;
import cn.com.startai.huadiantrainer.js.IJsRequestAnd;
import cn.com.startai.huadiantrainer.js.interfaceTask.CommonJsInterfaceTask;
import cn.com.startai.qxcommon.busi.huadian.AHDTrainerListener;
import cn.com.startai.qxcommon.busi.huadian.HDCmd;
import cn.com.startai.qxcommon.busi.huadian.HDTrainer;
import cn.com.startai.qxcommon.busi.huadian.bean.HDTextSpeakReq;
import cn.com.startai.qxcommon.link.mqtt.QXMqttHostManager;
import cn.com.startai.qxcommon.protocol.QXMessage;
import cn.com.startai.qxcommon.protocol.QXMessageConverter;
import cn.com.startai.qxcommon.utils.gson.QXJsonUtils;
import cn.com.swain.baselib.app.IApp.IService;
import cn.com.swain.baselib.jsInterface.AbsJsInterface;
import cn.com.swain.baselib.jsInterface.IotContent.request.DataContent;
import cn.com.swain.baselib.jsInterface.IotContent.response.ResponseMethodUtil;
import cn.com.swain.baselib.log.Tlog;

/**
 * author Guoqiang_Sun
 * date 2019/5/16
 * desc
 */
public class JsActivity implements IService, IJsInterfaceInstance,
        CommonJsInterfaceTask.IJsInterfaceRequest, ICallJS {

    public static final String TAG = AbsJsInterface.TAG;

    private Application app;
    private IJsRequestAnd mCallJS;
    private final HDTrainer mHDTrainer;

    public JsActivity(Application app, IJsRequestAnd mCallJS) {
        this.app = app;
        this.mCallJS = mCallJS;
        this.mHDTrainer = HDTrainer.getInstance();
    }

    private CommonJsInterfaceTask commonJsInterfaceTask;

    @Override
    public AbsJsInterface getJsInterface() {
        return commonJsInterfaceTask;
    }

    @Override
    public void onSCreate() {
        Tlog.d(TAG, "JsActivity onSCreate()");
        commonJsInterfaceTask = new CommonJsInterfaceTask(
                LooperManager.getInstance().getWorkLooper(),
                this);

        //内网测试 服务器;
//        String innerTestURL = "ssl://192.168.16.210:8883";
//        String cnURL = "ssl://cn.startai.net:8883";
//        QXMqttHostManager.HOST_FORCE = new String[]{innerTestURL, cnURL};

        mHDTrainer.init(app, new AHDTrainerListener() {
            @Override
            public void onHDMessageArrived(String msg) {
                Tlog.d(TAG, "[onHDMessageArrived] " + msg);
                callJsMethod(msg);
            }
        });


    }


    @Override
    public void onSResume() {
        Tlog.d(TAG, "JsActivity onSResume()");
    }

    @Override
    public void onSPause() {
        Tlog.d(TAG, "JsActivity onSPause()");
    }

    @Override
    public void onSFinish() {
        Tlog.d(TAG, "JsActivity onSFinish()");
    }

    @Override
    public void onSDestroy() {
        Tlog.d(TAG, "JsActivity onSDestroy()");
        if (commonJsInterfaceTask != null) {
            commonJsInterfaceTask.release();
            commonJsInterfaceTask = null;
        }
        app = null;
        mCallJS = null;
        mHDTrainer.release();
    }

    @Override
    public void handleJsRequest(DataContent mDataContent) {
        if (Debuger.isLogDebug) {
            Tlog.d(TAG, "[handleJsRequest]" + mDataContent);
        }
        long cmdByJson = mDataContent.getBusinessJsonBean().getCmdByJson();
        if (cmdByJson == HDCmd.SDK_RELEASE) {
            mCallJS.jFinish();
        } else if (cmdByJson == HDCmd.SDK_TEXT_SPEAKER) {
            String text = QXMessageConverter.getInstance().getFromJson(mDataContent.getRootJsonStr(), "text");
            SpeakManager.getInstance().speak(text);
        } else {
            mHDTrainer.doSend(mDataContent.getRootJsonStr());
        }
    }

    @Override
    public void onJsDataParseError(Exception e, String jsonData) {
        Tlog.e(TAG, "[onJsDataParseError]" + jsonData, e);
    }

    /**
     * 回调JS函数
     *
     * @param json 回调函数的参数，json字符串
     */
    protected void callJsMethod(String json) {
//        ResponseMethod mMethod = new ResponseMethod();
//        String method = mMethod.toJsMethod(json);
//        callJs(method);

        String method = ResponseMethodUtil.toJsMethod(json);
        callJs(method);
    }

    @Override
    public void callJs(String method) {
        if (mCallJS != null) {
            mCallJS.callJs(method);
        } else {
            Tlog.e(TAG, "callJs mCallJS is null. method:" + method);
        }
    }
}
